package com.wiki.controller;


import com.wiki.service.CategoryService;
import com.wiki.utils.commonRequest.category.CategoryQueryReq;
import com.wiki.utils.commonRequest.category.CategorySaveReq;
import com.wiki.utils.commonResponse.CategoryQueryResp;
import com.wiki.utils.commonResponse.CommonResp;
import com.wiki.utils.commonResponse.PageResp;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * <p>
 * 分类 前端控制器
 * </p>
 *
 * @author 偷闰土的猹
 * @since 2022-01-07
 */
@RestController
@RequestMapping("/category")
public class CategoryController {

    @Autowired
    CategoryService categoryService;


    @GetMapping("/list")
    public CommonResp list(@Valid CategoryQueryReq req) {
        CommonResp<PageResp<CategoryQueryResp>> listCommonResp = new CommonResp<>();
        PageResp<CategoryQueryResp> name = categoryService.listAll(req);
        listCommonResp.setContent(name);
        return listCommonResp;
    }

    @GetMapping("/list_by_name")
    public CommonResp list_by_name(@Valid CategoryQueryReq req) {
        CommonResp<PageResp<CategoryQueryResp>> listCommonResp = new CommonResp<>();
        PageResp<CategoryQueryResp> resp = categoryService.queryLike(req);
        listCommonResp.setContent(resp);
        return listCommonResp;
    }

    @PostMapping("/save")
    public CommonResp save(@Valid @RequestBody CategorySaveReq req) {
        CommonResp resp = new CommonResp();
        categoryService.save(req);
        return resp;
    }

    @DeleteMapping("/delete/{id}")
    public CommonResp delete(@PathVariable Long id) {
        CommonResp resp = new CommonResp();
        categoryService.delete(id);

        return resp;
    }

    @GetMapping("/all")
    public CommonResp all() {
        CommonResp<List<CategoryQueryResp>> resp = new CommonResp<>();
        List<CategoryQueryResp> all = categoryService.all();

        resp.setContent(all);
        return resp;
    }


}
